package com.zhouyu;

import com.zhouyu.bean.Lai;
import com.zhouyu.bean.LaiFactoryBean;
import com.zhouyu.service.UserInterface;
import com.zhouyu.service.UserService;
import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.aop.Pointcut;
import org.springframework.aop.PointcutAdvisor;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.StaticMethodMatcherPointcut;
import org.springframework.cglib.proxy.Callback;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodProxy;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class Test {

	public static void main(String[] args) throws IOException {

		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
		UserService userService = (UserService) context.getBean("userService");
		userService.test();
		// Proxy.newProxyInstance(UserService.class.getClassLoader(), new Class[]{UserInterface.class}, new InvocationHandler() {
		// 	@Override
		// 	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		// 		return null;
		// 	}
		// });
		// UserService target = new UserService();
		// //
		// ProxyFactory proxyFactory = new ProxyFactory();
		// proxyFactory.setTarget(target);
		// proxyFactory.addAdvisor(new PointcutAdvisor() {
		// 	@Override
		// 	public Pointcut getPointcut() {
		// 		return new StaticMethodMatcherPointcut() {
		// 			@Override
		// 			public boolean matches(Method method, Class<?> targetClass) {
		// 				//表示只有在test方法的时候才会产生增强
		// 				return method.getName().equals("test");
		// 			}
		// 		};
		// 	}
		//
		// 	@Override
		// 	public Advice getAdvice() {
		// 		return new MethodInterceptor() {
		// 			@Override
		// 			public Object invoke(MethodInvocation invocation) throws Throwable {
		// 				System.out.println("before...");
		// 				Object result = invocation.proceed();
		// 				System.out.println("after...");
		// 				return result;
		// 			}
		// 		};
		// 	}
		//
		// 	@Override
		// 	public boolean isPerInstance() {
		// 		return true;
		// 	}
		// });
		//
		// UserInterface userService = (UserInterface) proxyFactory.getProxy();
		// userService.test();

	}
}
